﻿using Arch.EntityFrameworkCore.UnitOfWork;
using AutoMapper;
using MyTodoApi.Context;
using Shared.ToDo.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyTodoApi.Services
{
    public class BaseService<TDto, T> : IBaseSevice<TDto, T> where TDto : BaseDto where T : BaseEntity
    {
        private readonly IUnitOfWork unitOfWork;
        private readonly IMapper mapper;

        public BaseService(IUnitOfWork unitOfWork, IMapper mapper)
        {
            this.unitOfWork = unitOfWork;
            this.mapper = mapper;
        }

        public async Task<ApiResponse> AddAsync(TDto dto)
        {
            try
            {
                var entity = mapper.Map<T>(dto);
                await unitOfWork.GetRepository<T>().InsertAsync(entity);
                var result = await unitOfWork.SaveChangesAsync();
                if (result > 0)
                {
                    return new ApiResponse(true, entity);
                }
                else
                {
                    return new ApiResponse(false, "保存失败");
                }
            }
            catch (Exception ex)
            {
                return new ApiResponse(false, ex.Message);
            }
        }

        public async Task<ApiResponse> Delete(int id)
        {
            try
            {
                //删除
                var repository = unitOfWork.GetRepository<TDto>();
                var entity = await repository.GetFirstOrDefaultAsync(predicate: x => x.ID.Equals(id));
                if (entity != null)
                {
                    repository.Delete(entity);
                }
                //保存
                var result = await unitOfWork.SaveChangesAsync();
                if (result > 0)
                {
                    return new ApiResponse(true, "");
                }
                else
                {
                    return new ApiResponse(false, "删除失败");
                }
            }
            catch (Exception ex)
            {
                return new ApiResponse(false, "删除失败" + ex.Message);
            }
        }

        public async Task<ApiResponse> GetAsync()
        {
            try
            {
                var repository = unitOfWork.GetRepository<TDto>();
                var results = await repository.GetAllAsync();
                return new ApiResponse(true, results);
            }
            catch (Exception ex)
            {
                return new ApiResponse(false, ex.Message);
            }
        }

        //public async Task<ApiResponse> GetAsyncPage()
        //{
        //    try
        //    {
        //        var repository = unitOfWork.GetRepository<T>();
        //        var results = await repository.GetPagedListAsync( ;
        //        return new ApiResponse(true, results);
        //    }
        //    catch (Exception ex)
        //    {
        //        return new ApiResponse(false, ex.Message);
        //    }
        //}



        public async Task<ApiResponse> GetById(int id)
        {
            try
            {
                //删除
                var repository = unitOfWork.GetRepository<TDto>();
                var entity = await repository.GetFirstOrDefaultAsync(predicate: x => x.ID.Equals(id));
                return new ApiResponse(true, entity);
            }
            catch (Exception ex)
            {
                return new ApiResponse(false, "删除失败" + ex.Message);
            }
        }

        public Task<ApiResponse> UpdateAsync(TDto entity)
        {
            throw new NotImplementedException();
        }
    }
}
